package com.icss.clothes.util;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

/**
 * 支付宝工具类
 */
public class AliPayUtil {
    private static AlipayClient alipayClient;
    static {
        alipayClient=new DefaultAlipayClient("https://openapi-sandbox.dl.alipaydev.com/gateway.do",
                "9021000136661802",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQ5wL3GMFTSYoq5WEofUM6/3PQcDPA1sKTAnYPa0wLNVosd+Qv0PCwtw4AmR4gMknbRGgipkAy9Km5ZgPFAinPCvV5znQNLxzssiOyCDf5k585+3V4KBV6e0D0X/V9wQkPWxdJt3zyTFVNjpAL2Y7VDZImfTzl1PVc4ajbnj4vFv7x2OT6mA4YZWSxahow28mIdJd1sAtXk9qrIa/1NRH+RYxOgx4DAl+BdeRZzLM0xmKdfDR/55wLDy6cBz8dtpZaNEQ35eb9uJax+fKpGTpfBEYouBQUUghi0eP8bG1spCytUpIe7gb/4w9pP74fo7wbug9xE/Ct+qOln/6QQex9AgMBAAECggEAZykwgT7MlEEs7vEzRKI5dQ1dJK5dChNbn1IHP18CWpB3TGVmvWwhj44jl+Vq/z0OO97tzI1T83MgSnAIiv/JSC/jJ/8FvEUocJM6uosDXtnsaFlP/mV7NSH+0nxmyErf6mDNqZvKhq41pOu0Pdt7xs7MmD5dVrtQm/nEP+bfGqJLDAFi6NQYc6EFw8QwuMnxQHHHCWv59GXSTLAmkOBeodLeyuWff668j0Q17TEG7NA+L2KPdDEtgMOEUXoy77JT9XcJ9DvhMJ1t1RM+PWYGkceMb9/MkmFL5oPf/2pjViEsMpZeriu+R6NEcV6SMEXVEpBIItMC9tmLn+BAg9B2OQKBgQDqLaBVYa+oLj0YZT+osNB9RkxbRvKaEj2+0sGoA4Cd0VwuFRUiaVlTCqiXuuloyqeVvTKud8IJ6z9y2k4IusP89yn5SXyKRhQBXdBYvDxGCustAXhutD8XTcAk5DPXaIP4d5porTLPh+QIBOJnsEKqwr4C5bMIPPURnfY2OzTWuwKBgQCeZ7C3Ii4r3dZL8Sm/1ZnBVQw+n4I+eP1czirV9FKKTs+1bocnXcFBPEa20zyuDfKgaea/LXEt/eA7SO+KMQLt+LOr3SmFjtF2W972qJLiOS3yV+SH5kqK8TswwC3F4/iC2NY7s34zpHPDoJ1Ff5Y84I3ni/yclSpiQS/SQHtCJwKBgQCoEtk5jqQhRBaLdNfMviLVVugktvYWSLNSSze8286q9oF/fZgum4gFxuecBwXRNWo0WZePitqUfeiLCU3qjAlwfQD+L5UYNRXUqT2JAokMCHgkVCLY6btCgYY/tB3VI+lcIpOLP4vxUWoDq2XH38TphF4V/ECT0BRgcYwt/i59UwKBgCSIj6Y9GN7/I+yR62oWDocbYV6UFUrpRq3/0rtTxTGm6ZrTqhI3OlocV8bUwzr1do7uvcw7482SrwOdV7nMgft2j6HEuU3XWZHtsGe9C1tniqRJBykcwPZ4moks2aG5aVhsQES2qhWpCRYGVh9wRKs3syLDE3jdok/U7v2T55mjAoGAFYQ/572z2ts0lfTpyv1reFTYjuCm/JsBpFkKcJdTG4xKzafuzRCS8kBDVTF2DGwWJ52aISGsjTSYEnVA1gl8qQMneFhOMcVa+CfzPcBzCjTewoIUoEStBvuV5Zlsy92f9fJeGIXt1zO8Wz0YWWKOoQ78rqjvpmPS1DkiJ2YfTzU=",
                "json",
                "GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkF71a66wG4SXMR8WRL5nvTJOaJcwoquJhnzCR/CUDJmc6T01CdQxIUQni2xMUdmvyjhIVDpUSWYaghCDqfW/eMl5AABOmdh1GPROVxxAn2aTUs4/2xAKVKxwk+cwc/h5qt0+L/o5jQvEWJ3mnqZZt4N2charDpwWx+6Bxi3SVD7lwQ5pE7msi+a6RMvlVmTnQlHzP0yh0AH6Vj7F8Rc6CvDUNdqBtHlyNlj3AyK/Lir64jFVtbjNOiZVCgZ6cdoPRUTbvM9OZZaSYujkVpVSr4N/0yIbn1oXfawEYcC0ws29XnlfMpfSkRp8Uvxgxj7IW5y+AKu6EbzbfZw9Ys3J3QIDAQAB",
                "RSA2");
    }
    public static String toPay(String order_id,double order_price)  {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl("");
//同步跳转地址，仅支持http/https
        request.setReturnUrl("http://localhost:8084/returnPay");
/******必传参数******/
        JSONObject bizContent = new JSONObject();
//商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", order_id);
//支付金额，最小值0.01元
        bizContent.put("total_amount", order_price);
//订单标题，不可使用特殊符号
        bizContent.put("subject", order_id);
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

/******可选参数******/
//bizContent.put("time_expire", "2022-08-01 22:00:00");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request,"POST");
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
// 如果需要返回GET请求，请使用
// AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return pageRedirectionData;
    }
}

